﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

namespace DAL
{
    [Serializable]
    public class SystemFrameworkData
    {

        public const string IDField = "@ID";

        public const string PRIMARYKEY_Field = @"ID";

        public const string SUPERIORIDField = "@SuperiorID";

        public const string FRAMELEVELField = "@FrameLevel";

        public const string NAMEField = "@Name";

        public const string CONTACTField = "@Contact";

        public const string ORGANIZATIONField = "@Organization";

        public const string ADDRESSField = "@Address";

        public const string MEASUREMENTCOUNTLIMITField = "@MeasurementCountLimit";

        public const string CURRENTMEASUREMENTCOUNTField = "@CurrentMeasurementCount";

        public const string TELEPHONEField = "@Telephone";

        public const string REMARKField = "@Remark";

        public const string CREATORField = "@Creator";

        public const string CREATETIMEField = "@CreateTime";

        public const string UPDATETIMEField = "@UpdateTime";

        private Guid _ID;

        private Guid _SuperiorID;

        private Int32 _FrameLevel;

        private String _Name;

        private String _Contact;

        private String _Organization;

        private String _Address;

        private Int32 _MeasurementCountLimit;

        private Int32 _CurrentMeasurementCount;

        private String _Telephone;

        private String _Remark;

        private String _Creator;

        private DateTime _CreateTime;

        private DateTime _UpdateTime;

        public Guid ID
        {
            get { return _ID; }
            set { _ID = value; }
        }
        public Guid SuperiorID
        {
            get { return _SuperiorID; }
            set { _SuperiorID = value; }
        }
        public Int32 FrameLevel
        {
            get { return _FrameLevel; }
            set { _FrameLevel = value; }
        }
        public String Name
        {
            get { return _Name; }
            set { _Name = value; }
        }
        public String Contact
        {
            get { return _Contact; }
            set { _Contact = value; }
        }
        public String Organization
        {
            get { return _Organization; }
            set { _Organization = value; }
        }
        public String Address
        {
            get { return _Address; }
            set { _Address = value; }
        }
        public Int32 MeasurementCountLimit
        {
            get { return _MeasurementCountLimit; }
            set { _MeasurementCountLimit = value; }
        }
        public Int32 CurrentMeasurementCount
        {
            get { return _CurrentMeasurementCount; }
            set { _CurrentMeasurementCount = value; }
        }
        public String Telephone
        {
            get { return _Telephone; }
            set { _Telephone = value; }
        }
        public String Remark
        {
            get { return _Remark; }
            set { _Remark = value; }
        }
        public String Creator
        {
            get { return _Creator; }
            set { _Creator = value; }
        }
        public DateTime CreateTime
        {
            get { return _CreateTime; }
            set { _CreateTime = value; }
        }
        public DateTime UpdateTime
        {
            get { return _UpdateTime; }
            set { _UpdateTime = value; }
        }
        public SystemFrameworkData()
        {
            _ID = Guid.Empty;
            _SuperiorID = Guid.Empty;
            _FrameLevel = 0;
            _Name = "";
            _Contact = "";
            _Organization = "";
            _Address = "";
            _MeasurementCountLimit = 1000;
            _CurrentMeasurementCount = 0;
            _Telephone = "";
            _Remark = "";
            _Creator = "";
            _CreateTime = DateTime.Now;
            _UpdateTime = DateTime.Now;
        }
    }

    public class SystemFrameworkDataFactory
    {
        public static SystemFrameworkData Construct(IDataReader reader)
        {
            SystemFrameworkData data = new SystemFrameworkData();

            try { data.ID = (Guid)(reader["ID"]); }
            catch { }

            try { data.SuperiorID = (Guid)(reader["SuperiorID"]); }
            catch { }

            try { data.FrameLevel = Convert.ToInt32(reader["FrameLevel"]); }
            catch { }

            try { data.Name = Convert.ToString(reader["Name"]); }
            catch { }

            try { data.Contact = Convert.ToString(reader["Contact"]); }
            catch { }

            try { data.Organization = Convert.ToString(reader["Organization"]); }
            catch { }

            try { data.Address = Convert.ToString(reader["Address"]); }
            catch { }

            try { data.MeasurementCountLimit = Convert.ToInt32(reader["MeasurementCountLimit"]); }
            catch { }

            try { data.CurrentMeasurementCount = Convert.ToInt32(reader["CurrentMeasurementCount"]); }
            catch { }

            try { data.Telephone = Convert.ToString(reader["Telephone"]); }
            catch { }

            try { data.Remark = Convert.ToString(reader["Remark"]); }
            catch { }

            try { data.Creator = Convert.ToString(reader["Creator"]); }
            catch { }

            try { data.CreateTime = Convert.ToDateTime(reader["CreateTime"]); }
            catch { }

            try { data.UpdateTime = Convert.ToDateTime(reader["UpdateTime"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataTable dt)
        {
            return GetPrimaryKey(dt, 0);
        }
        public static Guid GetPrimaryKey(DataTable dt, int rowIndex)
        {
            Guid PrimaryKey = Guid.Empty;

            if (dt.Rows.Count < rowIndex + 1)

                return Guid.Empty;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return Guid.Empty;

            try { PrimaryKey = (Guid)(dr[SystemFrameworkData.PRIMARYKEY_Field]); }
            catch { }

            return PrimaryKey;
        }
        public static SystemFrameworkData Construct(DataTable dt)
        {
            return Construct(dt, 0);
        }
        public static SystemFrameworkData Construct(DataTable dt, int rowIndex)
        {
            SystemFrameworkData data = new SystemFrameworkData();

            if (dt.Rows.Count < rowIndex + 1)

                return null;

            DataRow dr = dt.Rows[rowIndex];

            if (dr == null)

                return null;

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.SuperiorID = new Guid(dr["SuperiorID"].ToString()); }
            catch { }

            try { data.FrameLevel = Convert.ToInt32(dr["FrameLevel"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.Contact = Convert.ToString(dr["Contact"]); }
            catch { }

            try { data.Organization = Convert.ToString(dr["Organization"]); }
            catch { }

            try { data.Address = Convert.ToString(dr["Address"]); }
            catch { }

            try { data.MeasurementCountLimit = Convert.ToInt32(dr["MeasurementCountLimit"]); }
            catch { }

            try { data.CurrentMeasurementCount = Convert.ToInt32(dr["CurrentMeasurementCount"]); }
            catch { }

            try { data.Telephone = Convert.ToString(dr["Telephone"]); }
            catch { }

            try { data.Remark = Convert.ToString(dr["Remark"]); }
            catch { }

            try { data.Creator = Convert.ToString(dr["Creator"]); }
            catch { }

            try { data.CreateTime = Convert.ToDateTime(dr["CreateTime"]); }
            catch { }

            try { data.UpdateTime = Convert.ToDateTime(dr["UpdateTime"]); }
            catch { }

            return data;
        }
        public static SystemFrameworkData Construct(DataRow dr)
        {
            SystemFrameworkData data = new SystemFrameworkData();

            try { data.ID = new Guid(dr["ID"].ToString()); }
            catch { }

            try { data.SuperiorID = new Guid(dr["SuperiorID"].ToString()); }
            catch { }

            try { data.FrameLevel = Convert.ToInt32(dr["FrameLevel"]); }
            catch { }

            try { data.Name = Convert.ToString(dr["Name"]); }
            catch { }

            try { data.Contact = Convert.ToString(dr["Contact"]); }
            catch { }

            try { data.Organization = Convert.ToString(dr["Organization"]); }
            catch { }

            try { data.Address = Convert.ToString(dr["Address"]); }
            catch { }

            try { data.MeasurementCountLimit = Convert.ToInt32(dr["MeasurementCountLimit"]); }
            catch { }

            try { data.CurrentMeasurementCount = Convert.ToInt32(dr["CurrentMeasurementCount"]); }
            catch { }

            try { data.Telephone = Convert.ToString(dr["Telephone"]); }
            catch { }

            try { data.Remark = Convert.ToString(dr["Remark"]); }
            catch { }

            try { data.Creator = Convert.ToString(dr["Creator"]); }
            catch { }

            try { data.CreateTime = Convert.ToDateTime(dr["CreateTime"]); }
            catch { }

            try { data.UpdateTime = Convert.ToDateTime(dr["UpdateTime"]); }
            catch { }

            return data;
        }
        public static Guid GetPrimaryKey(DataRow dr)
        {
            Guid PrimaryKey = Guid.Empty;

            try { PrimaryKey = (Guid)(dr[SystemFrameworkData.PRIMARYKEY_Field]); }
            catch { }
            return PrimaryKey;
        }

    }
    public class SystemFrameworkDataDBOption
    {
        public static int Insert(SystemFrameworkData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("insert into SystemFramework (");
            sql.Append("SuperiorID, ");
            sql.Append("FrameLevel, ");
            sql.Append("Name, ");
            sql.Append("Contact, ");
            sql.Append("Organization, ");
            sql.Append("Address, ");
            sql.Append("MeasurementCountLimit, ");
            sql.Append("CurrentMeasurementCount, ");
            sql.Append("Telephone, ");
            sql.Append("Remark, ");
            sql.Append("Creator, ");
            sql.Append("CreateTime, ");
            sql.Append("UpdateTime");
            sql.Append(") values (");
            sql.Append("'");
            sql.Append(d.SuperiorID.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.FrameLevel.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Contact.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Organization.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Address.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.MeasurementCountLimit.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("");
            sql.Append(d.CurrentMeasurementCount.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Telephone.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Remark.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.Creator.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("'");
            sql.Append(d.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(")");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Update(SystemFrameworkData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("update SystemFramework set ");
            sql.Append("SuperiorID=");
            sql.Append("'");
            sql.Append(d.SuperiorID.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("FrameLevel=");
            sql.Append("");
            sql.Append(d.FrameLevel.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("Name=");
            sql.Append("'");
            sql.Append(d.Name.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Contact=");
            sql.Append("'");
            sql.Append(d.Contact.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Organization=");
            sql.Append("'");
            sql.Append(d.Organization.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Address=");
            sql.Append("'");
            sql.Append(d.Address.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("MeasurementCountLimit=");
            sql.Append("");
            sql.Append(d.MeasurementCountLimit.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("CurrentMeasurementCount=");
            sql.Append("");
            sql.Append(d.CurrentMeasurementCount.ToString());
            sql.Append("");
            sql.Append(", ");
            sql.Append("Telephone=");
            sql.Append("'");
            sql.Append(d.Telephone.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Remark=");
            sql.Append("'");
            sql.Append(d.Remark.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("Creator=");
            sql.Append("'");
            sql.Append(d.Creator.ToString());
            sql.Append("'");
            sql.Append(", ");
            sql.Append("CreateTime=");
            sql.Append("'");
            sql.Append(d.CreateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(", ");
            sql.Append("UpdateTime=");
            sql.Append("'");
            sql.Append(d.UpdateTime.ToString("yyyy-MM-dd HH:mm:ss"));
            sql.Append("'");
            sql.Append(" where ID='");
            sql.Append(d.ID.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static int Delete(SystemFrameworkData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from SystemFramework ");
            sql.Append(" where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
        public static DataTable DataTableSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework ");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataTable DataTableSelect(SystemFrameworkData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework where ID=");
            sql.Append(d.ID.ToString());
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }
        public static DataSet DataSetSelect()
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework ");
            return WFNetLib.ADO.SQLServerOP.DataSetSQL(sql.ToString());
        }
        public static SqlDataReader ReaderSelect(SystemFrameworkData d)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework ");
            return WFNetLib.ADO.SQLServerOP.ReaderExecuteSQL(sql.ToString());
        }


        public static DataTable DataTableSelect(string id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework where ID='");
            sql.Append(id);
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }


        public static DataTable SelectByLevel(int Level)
        {
            return SelectByLevel(Guid.Empty,Level);
        }
        public static DataTable SelectByLevel(Guid SuperiorID, int Level)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework where (FrameLevel=");
            sql.Append(Level.ToString());
            sql.Append(")");
            if (SuperiorID != Guid.Empty)
            {
                sql.Append(" and (SuperiorID='");
                sql.Append(SuperiorID.ToString());
                sql.Append("')");
            }
            return WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
        }

        public static SystemFrameworkData SelectByID(Guid ID)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("select * from SystemFramework where (ID='");
            sql.Append(ID.ToString());
            sql.Append("')");            
            DataTable dt= WFNetLib.ADO.SQLServerOP.DataTableSQL(sql.ToString());
            if (dt.Rows.Count == 0)
                return null;
            else
                return SystemFrameworkDataFactory.Construct(dt.Rows[0]);
        }

        public static int DeleteByID(Guid id)
        {
            StringBuilder sql = new StringBuilder();
            sql.Append("delete from SystemFramework ");
            sql.Append(" where ID='");
            sql.Append(id.ToString());
            sql.Append("'");
            return WFNetLib.ADO.SQLServerOP.NonQuerySQL(sql.ToString());
        }
    }
}